﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Wizard.WizardControl.Models;
using Wizard.BranchTestView;
using System.Collections.ObjectModel;
using Wizard.WizardControl;

namespace Wizard.BranchTestModel
{

    public class Step1Model : WizardStepViewModelBase<BranchTest>
    {

        private RouteOptionGroupViewModel<BranchTestEnum> _options;

        public RouteOptionGroupViewModel<BranchTestEnum> Options
        {
            get
            {
                if ( _options == null )
                {
                    CreateOptions();
                }
                return _options;
            }
        }

        public Step1Model( BranchTest businessObject )
            : base( businessObject )
        {
            CreateOptions();
        }


        private void CreateOptions()
        {
            var list = new List<RouteOptionViewModel<BranchTestEnum>>();
            list.Add( new RouteOptionViewModel<BranchTestEnum>( BranchTestEnum.Step2 ) );
            list.Add( new RouteOptionViewModel<BranchTestEnum>( BranchTestEnum.Step3 ) );
            list.ForEach( option =>
                {
                    option.PropertyChanged += ( s, e ) =>
                        {
                            var selectedOption = (RouteOptionViewModel<BranchTestEnum>)s;
                            if ( selectedOption.IsSelected )
                            {
                                list.ForEach( innerOption => innerOption.Messages.Clear() );
                                selectedOption.Messages.Add( "On next, following step will change to " + selectedOption.DisplayName );
                            }
                        };
                } );
            _options = new RouteOptionGroupViewModel<BranchTestEnum>( "Choose which step to jump to next" ) { OptionModels = new ReadOnlyCollection<RouteOptionViewModel<BranchTestEnum>>( list ) };
        }

        public override RouteModifier OnNext()
        {
            if ( GetSelectedOption().GetValue() == BranchTestEnum.Step2 )
            {
                return new RouteModifier { ExcludeViewTypes = new List<Type> { typeof( Step3 ) }, IncludeViewTypes = new List<Type> { typeof( Step2 ) } };
            }
            else
            {
                return new RouteModifier { ExcludeViewTypes = new List<Type> { typeof( Step2 ) }, IncludeViewTypes = new List<Type> { typeof( Step3 ) } };
            }
        }

        private RouteOptionViewModel<BranchTestEnum> GetSelectedOption()
        {
            return _options.OptionModels.FirstOrDefault( o => o.IsSelected );
        }

        public override string DisplayName
        {
            get { return "schtep 1"; }
        }

        public override bool IsValid()
        {
            return GetSelectedOption() != null;
        }
    }

}
